<template>
  <div v-loading="loading" class="app-container">
    <div class="head-container">
      <el-collapse v-model="activeName" accordion>
        <el-collapse-item name="1">
          <template slot="title">
            <i class="el-icon-search" />搜索栏
          </template>
          <el-form :model="params" class="searchform" :inline="true" size="mini" label-width="auto">
            <el-form-item label="作业类型">
              <el-select v-model="params.automationCode" placeholder="选择查询" clearable style="width: 200px" @change="toQuery">
                <el-option key="rekey" label="rekey" value="rekey" />
              </el-select>
            </el-form-item>
          </el-form>
        </el-collapse-item>
      </el-collapse>
    </div>
    <div style="padding-top:5px">
      <el-button v-if="params.automationCode" size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-plus" round @click="toApiInfo(null)">新增步骤</el-button>
      <el-table ref="itemTab" :data="data" stripe size="small" border style="width: 100%;" @header-dragend="colChange">
        <el-table-column :show-overflow-tooltip="true" label="执行顺序" prop="operationStep" :width="colWidths.operationStep" />
        <el-table-column :show-overflow-tooltip="true" label="作业类型" prop="automationCode" :width="colWidths.automationCode" />
        <el-table-column :show-overflow-tooltip="true" label="执行作业" prop="beanName" :width="colWidths.beanName" />
        <el-table-column :show-overflow-tooltip="true" label="异常后处理" prop="errorNext" :width="colWidths.errorNext">
          <template slot-scope="scope">
            <span v-if="scope.row.errorNext === 1">继续后续操作</span>
            <span v-if="scope.row.errorNext === 2">中止后续操作</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="left">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" round @click="toApiInfo(scope.row)">修改</el-button>
            <el-button v-if="scope.row.operationStep !== 1" size="mini" type="warning" round @click="toUp(scope)">上移</el-button>
            <el-button v-if="scope.row.operationStep < data.length" size="mini" type="warning" round @click="toDown(scope)">下移</el-button>
            <el-button size="mini" type="danger" round @click="doDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <apiInfoForm ref="apiInfoForm" :parent="parent" />
  </div>
</template>

<script>
import initData from '@/utils/initData'
import apiInfoForm from './apiInfo'
import { deleteStep, updateStepOrder } from '@/api/host'
import { Message } from 'element-ui'

export default {
  components: { apiInfoForm },
  mixins: [initData],
  data() {
    return {
      parent: this,
      loading: false,
      activeName: '1'
    }
  },
  methods: {
    beforeInit() {
      this.url = '/host/queryList'
      this.params.pageNum = this.page
      this.params.pageSize = this.size
      return true
    },
    // 查询
    toQuery() {
      this.page = 0
      this.currentPage = 1
      this.init()
    },
    // 弹出详情页
    toApiInfo(val) {
      const _apiInfo = this.$refs.apiInfoForm
      if (val) {
        _apiInfo.param = val
      } else {
        _apiInfo.param = { automationCode: this.params.automationCode }
      }
      _apiInfo.dialog = true
    },
    // 删除操作
    doDelete(val) {
      this.$confirm('确认删除该操作步骤?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        deleteStep(val).then(res => {
          this.toQuery()
          Message({
            message: '操作成功。',
            type: 'success',
            duration: 3 * 1000
          })
          this.loading = false
        }, error => {
          console.log(error)
          this.loading = false
        })
      })
    },
    // 上移
    toUp(val) {
      if (val.$index < 1) {
        Message({
          message: '最上节点无法上移。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('确认上移该操作步骤?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          const valIndex = val.row.operationStep
          const upObj = this.data[val.$index - 1]
          val.row.operationStep = upObj.operationStep
          upObj.operationStep = valIndex
          updateStepOrder([upObj, val.row]).then(res => {
            this.toQuery()
            Message({
              message: '操作成功。',
              type: 'success',
              duration: 3 * 1000
            })
            this.loading = false
          }, error => {
            console.log(error)
            this.loading = false
          })
        })
      }
    },
    // 上移
    toDown(val) {
      if (val.$index > this.data.length) {
        Message({
          message: '最下节点无法下移。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('确认下移该操作步骤?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          const valIndex = val.row.operationStep
          const upObj = this.data[val.$index + 1]
          val.row.operationStep = upObj.operationStep
          upObj.operationStep = valIndex
          updateStepOrder([upObj, val.row]).then(res => {
            this.toQuery()
            Message({
              message: '操作成功。',
              type: 'success',
              duration: 3 * 1000
            })
            this.loading = false
          }, error => {
            console.log(error)
            this.loading = false
          })
        })
      }
    }
  }
}
</script>
